(function(angular){
    'use strict';
    //创建模块
    var app = angular.module('myApp',['service']);
    //创建控制器
    app.controller('demoController',['$scope','$location','$window','myService',function($scope,$location,$window,myService){
        $scope.data =myService.getData();
        //2:添加任务
        $scope.newTodo="";
        $scope.add= function () {
            if($scope.newTodo === ""){
                return;
            }
             myService.add($scope.newTodo);
            //置空
            $scope.newTodo="";
        }

        //3:删除任务
      $scope.remove = function (id) {
         myService.remove(id);
      }
        //4:修改任务信息
        $scope.editingId =-1;
        $scope.edit = function (id) {
            $scope.editingId =id;
        }
        $scope.save = function (id) {
            $scope.editingId =-1;
            $scope.setData();
        }
        //5:切换任务完成与否
          $scope.changeStatus=function(){
              myService.setData();
          }
        //6:批量切换任务完成与否
        $scope.selectAll =false;
        $scope.toggleAll= function () {
           myService.toggleAll($scope.selectAll);
        }
        //7:显示未完成的任务数
        $scope.getActive = function () {
          return  myService.getActive();
        }
        //8:清除已经完成的任务
        $scope.clearAll = function () {
            myService.clearAll();
        }
        //9:切换显示不同的任务状态
        //$scope.local =$location;
        $scope.iscompleted ={};
        //$scope.all= function () {
        //     $scope.iscompleted ={}
        //}
        //$scope.active = function () {
        //    $scope.iscompleted={completed :false};
        //}
        //$scope.completed =function(){
        //    $scope.iscompleted ={completed:true};
        //}
        $scope.loca = $location;
        //console.log($location);
        $scope.$watch('loca.url()',function(now,old){
            switch (now){
                case '/active':
                    $scope.iscompleted={completed:false}
                    break;
                case '/completed':
                    $scope.iscompleted ={completed:true}
                    break;
                default:
                    $scope.iscompleted ={};
            }
        });


    }])
})(angular)
